{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "To store our data files, we create a temporary directory"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from implementations.classification_heads import LastTokenClassificationHead\n",
    "from implementations.transformers.basic_transformers import BasicTransformer\n",
    "\n",
    "demonstration_folder = \"demonstration\"\n",
    "\n",
    "if not os.path.exists(demonstration_folder):\n",
    "    os.mkdir(demonstration_folder)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:37.306970900Z",
     "start_time": "2023-08-22T15:41:36.923971600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "We begin by downloading the UNSW-NB15 flow dataset. We can obtain this from [here](https://staff.itee.uq.edu.au/marius/NIDS_datasets/)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:37.421470900Z",
     "start_time": "2023-08-22T15:41:36.932988400Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "zip_path = os.path.join(demonstration_folder, \"dataset.zip\")\n",
    "\n",
    "if not os.path.exists(zip_path):\n",
    "    r = requests.get(\"https://api.rdm.uq.edu.au/production/files/8c6e2a00-ef9c-11ed-827d-e762de186848/download\")\n",
    "\n",
    "    with open(zip_path, \"wb\") as w:\n",
    "        w.write(r.content)\n",
    "\n",
    "    print(f\"Downloaded file to {zip_path}, size = {os.path.getsize(zip_path):,}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can now extract the dataset"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset is available at demonstration\\dataset.csv, size = 441,867,785\n"
     ]
    }
   ],
   "source": [
    "import zipfile\n",
    "\n",
    "csv_path = os.path.join(demonstration_folder, \"dataset.csv\")\n",
    "\n",
    "if not os.path.exists(csv_path):\n",
    "    with zipfile.ZipFile(zip_path, \"r\") as zip_ref:\n",
    "        zip_ref.extract(\"fe6cb615d161452c_MOHANAD_A4706/data/NF-UNSW-NB15-v2.csv\", demonstration_folder)\n",
    "        inner_path = os.path.join(demonstration_folder, \"fe6cb615d161452c_MOHANAD_A4706/data/NF-UNSW-NB15-v2.csv\")\n",
    "        os.rename(inner_path, csv_path)\n",
    "\n",
    "print(f\"Dataset is available at {csv_path}, size = {os.path.getsize(csv_path):,}\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:37.298471200Z",
     "start_time": "2023-08-22T15:41:36.987470600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can now define the dataset format, which will allow us to ingest this dataset with FlowTransformer"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [],
   "source": [
    "from framework.dataset_specification import DatasetSpecification\n",
    "\n",
    "flow_format = DatasetSpecification(\n",
    "        include_fields=['NUM_PKTS_UP_TO_128_BYTES', 'SRC_TO_DST_SECOND_BYTES', 'OUT_PKTS', 'OUT_BYTES', 'NUM_PKTS_128_TO_256_BYTES', 'DST_TO_SRC_AVG_THROUGHPUT', 'DURATION_IN', 'L4_SRC_PORT', 'ICMP_TYPE', 'PROTOCOL', 'SERVER_TCP_FLAGS', 'IN_PKTS', 'NUM_PKTS_512_TO_1024_BYTES', 'CLIENT_TCP_FLAGS', 'TCP_WIN_MAX_IN', 'NUM_PKTS_256_TO_512_BYTES', 'SHORTEST_FLOW_PKT', 'MIN_IP_PKT_LEN', 'LONGEST_FLOW_PKT', 'L4_DST_PORT', 'MIN_TTL', 'DST_TO_SRC_SECOND_BYTES', 'NUM_PKTS_1024_TO_1514_BYTES', 'DURATION_OUT', 'FLOW_DURATION_MILLISECONDS', 'TCP_FLAGS', 'MAX_TTL', 'SRC_TO_DST_AVG_THROUGHPUT', 'ICMP_IPV4_TYPE', 'MAX_IP_PKT_LEN', 'RETRANSMITTED_OUT_BYTES', 'IN_BYTES', 'RETRANSMITTED_IN_BYTES', 'TCP_WIN_MAX_OUT', 'L7_PROTO', 'RETRANSMITTED_OUT_PKTS', 'RETRANSMITTED_IN_PKTS'],\n",
    "        categorical_fields=['CLIENT_TCP_FLAGS', 'L4_SRC_PORT', 'TCP_FLAGS', 'ICMP_IPV4_TYPE', 'ICMP_TYPE', 'PROTOCOL', 'SERVER_TCP_FLAGS', 'L4_DST_PORT', 'L7_PROTO'],\n",
    "        class_column=\"Attack\",\n",
    "        benign_label=\"Benign\"\n",
    "    )"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:37.299971800Z",
     "start_time": "2023-08-22T15:41:37.005471100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "We can now set up a version of FlowTransformer:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [],
   "source": [
    "from framework.flow_transformer_parameters import FlowTransformerParameters\n",
    "from framework.flow_transformer import FlowTransformer\n",
    "from implementations.input_encodings import RecordLevelEmbed\n",
    "from implementations.pre_processings import StandardPreProcessing\n",
    "\n",
    "# We use several standard component to build our transformer\n",
    "pre_processing = StandardPreProcessing(n_categorical_levels=32)\n",
    "encoding = RecordLevelEmbed(64)\n",
    "transformer = BasicTransformer(n_layers=2, internal_size=128, n_heads=2)\n",
    "classification_head = LastTokenClassificationHead()\n",
    "\n",
    "# Define the transformer\n",
    "ft = FlowTransformer(pre_processing=pre_processing,\n",
    "                     input_encoding=encoding,\n",
    "                     sequential_model=transformer,\n",
    "                     classification_head=classification_head,\n",
    "                     params=FlowTransformerParameters(window_size=8, mlp_layer_sizes=[128], mlp_dropout=0.1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:37.304972Z",
     "start_time": "2023-08-22T15:41:37.021471200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "After defining the transformer architecture, we can ingest the dataset using the FlowTransformer instance, which will process the dataset into a normalised format (partitioning training and testing data, to ensure only the testing data is used to fit the pre-processing):"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using cache file path: demonstration\\UNSW-NB15_0_QdLmZHuh8yOmlGcKBEkf7hepImY0_5EjmvToFWKee8t20u0dFpVzNu4s0.feather\n",
      "Reading directly from cache demonstration\\UNSW-NB15_0_QdLmZHuh8yOmlGcKBEkf7hepImY0_5EjmvToFWKee8t20u0dFpVzNu4s0.feather...\n"
     ]
    },
    {
     "data": {
      "text/plain": "     SHORTEST_FLOW_PKT  DURATION_IN  NUM_PKTS_1024_TO_1514_BYTES  \\\n0             0.440913          0.0                          0.0   \n1             0.440913          0.0                          0.0   \n2             0.440913          0.0                          0.0   \n3             0.440913          0.0                          0.0   \n4             0.440913          0.0                          0.0   \n..                 ...          ...                          ...   \n495           0.440913          0.0                          0.0   \n496           0.440913          0.0                          0.0   \n497           0.440913          0.0                          0.0   \n498           0.440913          0.0                          0.0   \n499           0.440913          0.0                          0.0   \n\n     NUM_PKTS_128_TO_256_BYTES  TCP_WIN_MAX_OUT   MAX_TTL  \\\n0                          0.0         0.801390  0.630549   \n1                          0.0         0.817828  0.630549   \n2                          0.0         0.831726  0.630549   \n3                          0.0         0.843766  0.630549   \n4                          0.0         0.854385  0.630549   \n..                         ...              ...       ...   \n495                        0.0         0.863885  0.630549   \n496                        0.0         0.817828  0.630549   \n497                        0.0         0.872479  0.630549   \n498                        0.0         0.831726  0.630549   \n499                        0.0         0.843766  0.630549   \n\n     NUM_PKTS_UP_TO_128_BYTES  OUT_PKTS  IN_BYTES  MIN_TTL  ...  L7_PROTO_23  \\\n0                    0.326896  0.148859  0.127562    0.625  ...            0   \n1                    0.374701  0.223288  0.323054    0.625  ...            0   \n2                    0.408620  0.266827  0.358547    0.625  ...            0   \n3                    0.434929  0.297718  0.380413    0.625  ...            0   \n4                    0.465863  0.331913  0.402810    0.625  ...            0   \n..                        ...       ...       ...      ...  ...          ...   \n495                  0.482735  0.349851  0.414039    0.625  ...            0   \n496                  0.374701  0.223288  0.323054    0.625  ...            0   \n497                  0.497492  0.365217  0.424305    0.625  ...            0   \n498                  0.408620  0.266827  0.358547    0.625  ...            0   \n499                  0.434929  0.297718  0.380413    0.625  ...            0   \n\n     L7_PROTO_24  L7_PROTO_25  L7_PROTO_26  L7_PROTO_27  L7_PROTO_28  \\\n0              0            0            0            0            0   \n1              0            0            0            0            0   \n2              0            0            0            0            0   \n3              0            0            0            0            0   \n4              0            0            0            0            0   \n..           ...          ...          ...          ...          ...   \n495            0            0            0            0            0   \n496            0            0            0            0            0   \n497            0            0            0            0            0   \n498            0            0            0            0            0   \n499            0            0            0            0            0   \n\n     L7_PROTO_29  L7_PROTO_30  L7_PROTO_31  L7_PROTO_32  \n0              0            0            0            0  \n1              0            0            0            0  \n2              0            0            0            0  \n3              0            0            0            0  \n4              0            0            0            0  \n..           ...          ...          ...          ...  \n495            0            0            0            0  \n496            0            0            0            0  \n497            0            0            0            0  \n498            0            0            0            0  \n499            0            0            0            0  \n\n[500 rows x 268 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>SHORTEST_FLOW_PKT</th>\n      <th>DURATION_IN</th>\n      <th>NUM_PKTS_1024_TO_1514_BYTES</th>\n      <th>NUM_PKTS_128_TO_256_BYTES</th>\n      <th>TCP_WIN_MAX_OUT</th>\n      <th>MAX_TTL</th>\n      <th>NUM_PKTS_UP_TO_128_BYTES</th>\n      <th>OUT_PKTS</th>\n      <th>IN_BYTES</th>\n      <th>MIN_TTL</th>\n      <th>...</th>\n      <th>L7_PROTO_23</th>\n      <th>L7_PROTO_24</th>\n      <th>L7_PROTO_25</th>\n      <th>L7_PROTO_26</th>\n      <th>L7_PROTO_27</th>\n      <th>L7_PROTO_28</th>\n      <th>L7_PROTO_29</th>\n      <th>L7_PROTO_30</th>\n      <th>L7_PROTO_31</th>\n      <th>L7_PROTO_32</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.801390</td>\n      <td>0.630549</td>\n      <td>0.326896</td>\n      <td>0.148859</td>\n      <td>0.127562</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.817828</td>\n      <td>0.630549</td>\n      <td>0.374701</td>\n      <td>0.223288</td>\n      <td>0.323054</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.831726</td>\n      <td>0.630549</td>\n      <td>0.408620</td>\n      <td>0.266827</td>\n      <td>0.358547</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.843766</td>\n      <td>0.630549</td>\n      <td>0.434929</td>\n      <td>0.297718</td>\n      <td>0.380413</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.854385</td>\n      <td>0.630549</td>\n      <td>0.465863</td>\n      <td>0.331913</td>\n      <td>0.402810</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>495</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.863885</td>\n      <td>0.630549</td>\n      <td>0.482735</td>\n      <td>0.349851</td>\n      <td>0.414039</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>496</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.817828</td>\n      <td>0.630549</td>\n      <td>0.374701</td>\n      <td>0.223288</td>\n      <td>0.323054</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>497</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.872479</td>\n      <td>0.630549</td>\n      <td>0.497492</td>\n      <td>0.365217</td>\n      <td>0.424305</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>498</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.831726</td>\n      <td>0.630549</td>\n      <td>0.408620</td>\n      <td>0.266827</td>\n      <td>0.358547</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>499</th>\n      <td>0.440913</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.843766</td>\n      <td>0.630549</td>\n      <td>0.434929</td>\n      <td>0.297718</td>\n      <td>0.380413</td>\n      <td>0.625</td>\n      <td>...</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>500 rows × 268 columns</p>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from framework.enumerations import EvaluationDatasetSampling\n",
    "from IPython.display import display\n",
    "\n",
    "df = ft.load_dataset(\"UNSW-NB15\",\n",
    "                csv_path,\n",
    "                specification=flow_format,\n",
    "                evaluation_dataset_sampling=EvaluationDatasetSampling.LastRows,\n",
    "                evaluation_percent=0.1,\n",
    "                cache_path=demonstration_folder)\n",
    "\n",
    "display(df.iloc[:500])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:38.135972500Z",
     "start_time": "2023-08-22T15:41:37.035982Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "We are now ready to build the transformer model, that is able to ingest this dataset"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model_1\"\n",
      "__________________________________________________________________________________________________\n",
      " Layer (type)                   Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      " input_SHORTEST_FLOW_PKT (Input  [(None, 8, 1)]      0           []                               \n",
      " Layer)                                                                                           \n",
      "                                                                                                  \n",
      " input_DURATION_IN (InputLayer)  [(None, 8, 1)]      0           []                               \n",
      "                                                                                                  \n",
      " input_NUM_PKTS_1024_TO_1514_BY  [(None, 8, 1)]      0           []                               \n",
      " TES (InputLayer)                                                                                 \n",
      "                                                                                                  \n",
      " input_NUM_PKTS_128_TO_256_BYTE  [(None, 8, 1)]      0           []                               \n",
      " S (InputLayer)                                                                                   \n",
      "                                                                                                  \n",
      " input_TCP_WIN_MAX_OUT (InputLa  [(None, 8, 1)]      0           []                               \n",
      " yer)                                                                                             \n",
      "                                                                                                  \n",
      " input_MAX_TTL (InputLayer)     [(None, 8, 1)]       0           []                               \n",
      "                                                                                                  \n",
      " input_NUM_PKTS_UP_TO_128_BYTES  [(None, 8, 1)]      0           []                               \n",
      "  (InputLayer)                                                                                    \n",
      "                                                                                                  \n",
      " input_OUT_PKTS (InputLayer)    [(None, 8, 1)]       0           []                               \n",
      "                                                                                                  \n",
      " input_IN_BYTES (InputLayer)    [(None, 8, 1)]       0           []                               \n",
      "                                                                                                  \n",
      " input_MIN_TTL (InputLayer)     [(None, 8, 1)]       0           []                               \n",
      "                                                                                                  \n",
      " input_MAX_IP_PKT_LEN (InputLay  [(None, 8, 1)]      0           []                               \n",
      " er)                                                                                              \n",
      "                                                                                                  \n",
      " input_RETRANSMITTED_IN_BYTES (  [(None, 8, 1)]      0           []                               \n",
      " InputLayer)                                                                                      \n",
      "                                                                                                  \n",
      " input_TCP_WIN_MAX_IN (InputLay  [(None, 8, 1)]      0           []                               \n",
      " er)                                                                                              \n",
      "                                                                                                  \n",
      " input_MIN_IP_PKT_LEN (InputLay  [(None, 8, 1)]      0           []                               \n",
      " er)                                                                                              \n",
      "                                                                                                  \n",
      " input_SRC_TO_DST_SECOND_BYTES   [(None, 8, 1)]      0           []                               \n",
      " (InputLayer)                                                                                     \n",
      "                                                                                                  \n",
      " input_IN_PKTS (InputLayer)     [(None, 8, 1)]       0           []                               \n",
      "                                                                                                  \n",
      " input_NUM_PKTS_512_TO_1024_BYT  [(None, 8, 1)]      0           []                               \n",
      " ES (InputLayer)                                                                                  \n",
      "                                                                                                  \n",
      " input_DST_TO_SRC_AVG_THROUGHPU  [(None, 8, 1)]      0           []                               \n",
      " T (InputLayer)                                                                                   \n",
      "                                                                                                  \n",
      " input_LONGEST_FLOW_PKT (InputL  [(None, 8, 1)]      0           []                               \n",
      " ayer)                                                                                            \n",
      "                                                                                                  \n",
      " input_DURATION_OUT (InputLayer  [(None, 8, 1)]      0           []                               \n",
      " )                                                                                                \n",
      "                                                                                                  \n",
      " input_RETRANSMITTED_OUT_BYTES   [(None, 8, 1)]      0           []                               \n",
      " (InputLayer)                                                                                     \n",
      "                                                                                                  \n",
      " input_RETRANSMITTED_IN_PKTS (I  [(None, 8, 1)]      0           []                               \n",
      " nputLayer)                                                                                       \n",
      "                                                                                                  \n",
      " input_FLOW_DURATION_MILLISECON  [(None, 8, 1)]      0           []                               \n",
      " DS (InputLayer)                                                                                  \n",
      "                                                                                                  \n",
      " input_RETRANSMITTED_OUT_PKTS (  [(None, 8, 1)]      0           []                               \n",
      " InputLayer)                                                                                      \n",
      "                                                                                                  \n",
      " input_OUT_BYTES (InputLayer)   [(None, 8, 1)]       0           []                               \n",
      "                                                                                                  \n",
      " input_SRC_TO_DST_AVG_THROUGHPU  [(None, 8, 1)]      0           []                               \n",
      " T (InputLayer)                                                                                   \n",
      "                                                                                                  \n",
      " input_DST_TO_SRC_SECOND_BYTES   [(None, 8, 1)]      0           []                               \n",
      " (InputLayer)                                                                                     \n",
      "                                                                                                  \n",
      " input_NUM_PKTS_256_TO_512_BYTE  [(None, 8, 1)]      0           []                               \n",
      " S (InputLayer)                                                                                   \n",
      "                                                                                                  \n",
      " input_CLIENT_TCP_FLAGS (InputL  [(None, 8, 16)]     0           []                               \n",
      " ayer)                                                                                            \n",
      "                                                                                                  \n",
      " input_L4_SRC_PORT (InputLayer)  [(None, 8, 32)]     0           []                               \n",
      "                                                                                                  \n",
      " input_TCP_FLAGS (InputLayer)   [(None, 8, 17)]      0           []                               \n",
      "                                                                                                  \n",
      " input_ICMP_IPV4_TYPE (InputLay  [(None, 8, 32)]     0           []                               \n",
      " er)                                                                                              \n",
      "                                                                                                  \n",
      " input_ICMP_TYPE (InputLayer)   [(None, 8, 32)]      0           []                               \n",
      "                                                                                                  \n",
      " input_PROTOCOL (InputLayer)    [(None, 8, 32)]      0           []                               \n",
      "                                                                                                  \n",
      " input_SERVER_TCP_FLAGS (InputL  [(None, 8, 15)]     0           []                               \n",
      " ayer)                                                                                            \n",
      "                                                                                                  \n",
      " input_L4_DST_PORT (InputLayer)  [(None, 8, 32)]     0           []                               \n",
      "                                                                                                  \n",
      " input_L7_PROTO (InputLayer)    [(None, 8, 32)]      0           []                               \n",
      "                                                                                                  \n",
      " feature_concat (Concatenate)   (None, 8, 268)       0           ['input_SHORTEST_FLOW_PKT[0][0]',\n",
      "                                                                  'input_DURATION_IN[0][0]',      \n",
      "                                                                  'input_NUM_PKTS_1024_TO_1514_BYT\n",
      "                                                                 ES[0][0]',                       \n",
      "                                                                  'input_NUM_PKTS_128_TO_256_BYTES\n",
      "                                                                 [0][0]',                         \n",
      "                                                                  'input_TCP_WIN_MAX_OUT[0][0]',  \n",
      "                                                                  'input_MAX_TTL[0][0]',          \n",
      "                                                                  'input_NUM_PKTS_UP_TO_128_BYTES[\n",
      "                                                                 0][0]',                          \n",
      "                                                                  'input_OUT_PKTS[0][0]',         \n",
      "                                                                  'input_IN_BYTES[0][0]',         \n",
      "                                                                  'input_MIN_TTL[0][0]',          \n",
      "                                                                  'input_MAX_IP_PKT_LEN[0][0]',   \n",
      "                                                                  'input_RETRANSMITTED_IN_BYTES[0]\n",
      "                                                                 [0]',                            \n",
      "                                                                  'input_TCP_WIN_MAX_IN[0][0]',   \n",
      "                                                                  'input_MIN_IP_PKT_LEN[0][0]',   \n",
      "                                                                  'input_SRC_TO_DST_SECOND_BYTES[0\n",
      "                                                                 ][0]',                           \n",
      "                                                                  'input_IN_PKTS[0][0]',          \n",
      "                                                                  'input_NUM_PKTS_512_TO_1024_BYTE\n",
      "                                                                 S[0][0]',                        \n",
      "                                                                  'input_DST_TO_SRC_AVG_THROUGHPUT\n",
      "                                                                 [0][0]',                         \n",
      "                                                                  'input_LONGEST_FLOW_PKT[0][0]', \n",
      "                                                                  'input_DURATION_OUT[0][0]',     \n",
      "                                                                  'input_RETRANSMITTED_OUT_BYTES[0\n",
      "                                                                 ][0]',                           \n",
      "                                                                  'input_RETRANSMITTED_IN_PKTS[0][\n",
      "                                                                 0]',                             \n",
      "                                                                  'input_FLOW_DURATION_MILLISECOND\n",
      "                                                                 S[0][0]',                        \n",
      "                                                                  'input_RETRANSMITTED_OUT_PKTS[0]\n",
      "                                                                 [0]',                            \n",
      "                                                                  'input_OUT_BYTES[0][0]',        \n",
      "                                                                  'input_SRC_TO_DST_AVG_THROUGHPUT\n",
      "                                                                 [0][0]',                         \n",
      "                                                                  'input_DST_TO_SRC_SECOND_BYTES[0\n",
      "                                                                 ][0]',                           \n",
      "                                                                  'input_NUM_PKTS_256_TO_512_BYTES\n",
      "                                                                 [0][0]',                         \n",
      "                                                                  'input_CLIENT_TCP_FLAGS[0][0]', \n",
      "                                                                  'input_L4_SRC_PORT[0][0]',      \n",
      "                                                                  'input_TCP_FLAGS[0][0]',        \n",
      "                                                                  'input_ICMP_IPV4_TYPE[0][0]',   \n",
      "                                                                  'input_ICMP_TYPE[0][0]',        \n",
      "                                                                  'input_PROTOCOL[0][0]',         \n",
      "                                                                  'input_SERVER_TCP_FLAGS[0][0]', \n",
      "                                                                  'input_L4_DST_PORT[0][0]',      \n",
      "                                                                  'input_L7_PROTO[0][0]']         \n",
      "                                                                                                  \n",
      " embed (Dense)                  (None, 8, 64)        17216       ['feature_concat[0][0]']         \n",
      "                                                                                                  \n",
      " block_0_transformer_encoder (T  (None, 8, 64)       83200       ['embed[0][0]']                  \n",
      " ransformerEncoderBlock)                                                                          \n",
      "                                                                                                  \n",
      " block_1_transformer_encoder (T  (None, 8, 64)       83200       ['block_0_transformer_encoder[0][\n",
      " ransformerEncoderBlock)                                         0]']                             \n",
      "                                                                                                  \n",
      " slice_last (Lambda)            (None, 64)           0           ['block_1_transformer_encoder[0][\n",
      "                                                                 0]']                             \n",
      "                                                                                                  \n",
      " classification_mlp_0_128 (Dens  (None, 128)         8320        ['slice_last[0][0]']             \n",
      " e)                                                                                               \n",
      "                                                                                                  \n",
      " dropout_1 (Dropout)            (None, 128)          0           ['classification_mlp_0_128[0][0]'\n",
      "                                                                 ]                                \n",
      "                                                                                                  \n",
      " binary_classification_out (Den  (None, 1)           129         ['dropout_1[0][0]']              \n",
      " se)                                                                                              \n",
      "                                                                                                  \n",
      "==================================================================================================\n",
      "Total params: 192,065\n",
      "Trainable params: 192,065\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# Build the transformer model\n",
    "m = ft.build_model()\n",
    "m.summary()\n",
    "\n",
    "# Compile the model\n",
    "m.compile(optimizer=\"adam\", loss='binary_crossentropy', metrics=['binary_accuracy'], jit_compile=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-22T15:41:39.069970600Z",
     "start_time": "2023-08-22T15:41:38.140971600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "This model can now be used for machine learning training"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
